package com.example.cooldemo;

import android.os.Bundle;
import android.support.v7.widget.RecyclerView;

import com.chad.library.adapter.base.BaseViewHolder;
import com.example.cool_core.adapter.BaseAdapter;
import com.example.cool_core.adapter.OnBaseAdapterListener;
import com.example.cool_core.config.CoolViewConfig;
import com.ld.cool_library.http.CoolResObserver;
import com.ld.cool_library.http.HttpResModel;
import com.ld.cool_library.http.RxHttp;
import com.ld.cool_library.http.model.DemoModel;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class TopBaseHttp3Activity extends BaseActivity {
    private SmartRefreshLayout smartRefreshLayout;
    private RecyclerView rv_list;
    private BaseAdapter<DemoModel.DataBean> mAdapter;

    @Override
    protected int setLayoutId(Bundle savedInstanceState) {
        CoolViewConfig.getInstance()
                .setDefault();
        return R.layout.activity_top_base2;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        smartRefreshLayout = findViewById(R.id.refresh_layout);
        rv_list = findViewById(R.id.rv_list);
        mPublicConfig.setTopBar("demo");
        setRefresh(smartRefreshLayout, true);
        initAdapter();
        mRefreshUtil.onRefresh();
    }

    private void initAdapter() {
        mAdapter = new BaseAdapter.Builder<DemoModel.DataBean>(rv_list, mActivity, R.layout.adapter_demo)
                .build(new OnBaseAdapterListener<DemoModel.DataBean>() {
                    @Override
                    public void convert(BaseViewHolder helper, DemoModel.DataBean item) {
                        helper.setText(R.id.tv_name, item.getContent());
                    }
                });
    }

    @Override
    public void onHttpRefresh(int page) {
        super.onHttpRefresh(page);
        RxHttp.init().list(page + "", "1418816972", "3741f5d13dc389e3d0ae68cfd8571725")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new CoolResObserver<HttpResModel<DemoModel>>(mActivity, mPublicConfig) {
                    @Override
                    protected void onSuccess(HttpResModel<DemoModel> demoModelHttpResModel) {
                        setRefreshData(mAdapter, demoModelHttpResModel.getResult().getData());
                    }
                });
    }
}
